var isInitBuffer = false;
var lastProcess = 0;
var playMode = 4;
var seekTime = 0;
var curPlayData=false;
function play(data){
	if(_isAli){
		IMPlayer.Stop();
	}
	if(curPlayData){
		var cur = JP.getCurTime();
		var dur = JP.getDuration();
		if(cur>0 && cur<=(dur - 10)){		//中间换台
			seekObj[curPlayData.curItemId] = cur;
		}
	}
	curPlayData = data;
	curPlayData.curItemId = curData.curItemId;
	
	showBox();
	isInitBuffer = true;
	lastPercent = 0;
	seekTime = 0;
	var seek = seekObj[curData.curItemId]; 
	if(seek){
		seekTime = parseInt(seek);
		showSeekBox(seekTime);
	}

	try {	
		IMPlayer.Play(data.playUrl,playMode);
	}catch (e) {
        $.log(e);
	}
}

function OnStateChanged(state, percent,speed){
	myPlayer.state = state;  
	if ("play" == state) {
		if(isInitBuffer){
			if(seekTime > 0){
				JP.seek(seekTime);
				seekTime = 0 ; 
			}
			hideBox();
			hideBackground(); 
			isInitBuffer = false;
		}
	}else if("buffer" == state){
		if(isInitBuffer){
			if(percent > lastProcess){lastProcess = percent;}
			changeBox(lastProcess,speed);
		}else{
		
		}
	}else if("end" == state){
		curData.curItemId ++;
		if(curData.curItemId > curData.list.length){
			KEY.KEY_RETURN ();
		}else{
			playVod();
		}
	}
}

function changeBox(per,speed){
	if(per>0){
		$('#box .tip').html(lang.play.download);
	}
	if(speed>0){
		$('#box .speed').html(speed+'kb/s');
	}
	$('#box .buffer').html(per+'%');
}

function showBox(){
	var title = curData.title;
	if(curData.list.length >1){
		title += '('+curData.curItemId+')';
	}
	var img = $.getImageUrl(curData.img,DEFAULT_IMG);
	
	var con = '';
	con += '<div class="title">'+title+'</div>';
	con += '<div class="icon"><img src="'+img+'"/></div>';
	con += '<div class="tip">'+lang.play.init+'</div>';
	con += '<div class="buffer">0%</div>';
	con += '<div class="speed">0kb/s</div>';
	$('#playInfo').html(con);
	$('#lastBox').hide();
	$('#playBox').show();
}

function hideBox(){
	hideSeekBox();
	$('#box').hide();
}


var hasHideBackground = false;
function hideBackground(){
	if(!hasHideBackground){
		
		$('#pagewrap').attr("style","background: url();").hide();
		hasHideBackground = true;
	}
}


var hasInitSeekBox = false;
function initSeekBox(){
	$('#resumeA').html(lang.play.resume);
	$('#playBackA').html(lang.play.playBack);
	$('#playBox a').focus(function(e) {
        KEY.CURTYPE = 'playback';
    }).click(function(e) {
        var tar = $(this).attr('tar');
		if(tar == 'resume'){		//重新播放
			delete  seekObj[curData.curItemId];
			$.log($.toJSON(seekObj));
			var data = curData.list[curData.curItemId - 1];
			play(data);
		}
		hideSeekBox();
		return false;
    });;
}

function showSeekBox(time){
	if(!hasInitSeekBox){
		initSeekBox();
		hasInitSeekBox = true;
	}
	var h = parseInt(time/3600);
	time = time%3600;
	var m = parseInt(time/60);
	s = time%60;
	var t = '';
	if(h>0){
		t+= h>9?h:'0'+h;
		t+=':';
	}
	t+= m>9?m:'0'+m;
	t+=':';
	t+= m>9?m:'0'+m;
	$('#lastBox .lastTip').html(lang.play.lastWatch+':'+t);
	
	$('#lastBox').show();
	$('#resumeA').focus();
}

function hideSeekBox(){
	$('#justForA').focus();
	KEY.CURTYPE = false;
	
	$('#lastBox').hide();
}

KEY.TYPE['playback'] = function(key,keyName,a){
	var id = a.id;
	if(keyName == 'KEY_RETURN'){
		KEY.KEY_RETURN();
	}else if(keyName == 'KEY_RIGHT'){
		if(id == 'resumeA'){
			$('#playBackA').focus();
		}
	}else if(keyName == 'KEY_LEFT'){
		if(id == 'playBackA'){
			$('#resumeA').focus();
		}
	}else{
		if(KEY[keyName]){
			KEY[keyName](key,keyName,a);
		}
	}
}